package com.hanma.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hanma.bean.User;
import com.hanma.service.UserService;
import com.hanma.service.impl.UserServiceImpl;

@WebServlet({"/add.do","/list.do","/addAction.do","/update.do","/delete.do","/updateAction.do"})
//后端用户管理页面
public class UserServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        req.setCharacterEncoding("utf-8");
        UserService service = new UserServiceImpl();
        HttpSession session = req.getSession();
        //添加用户
        if (path.equals("/add.do")) {
            req.getRequestDispatcher("/manage/user/add.jsp").forward(req, resp);
        }
        //添加用户提交
        else if (path.equals("/addAction.do")) {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String con_password = req.getParameter("con_password");
            String head = "/message/home/img/4.jpeg";
            User user = new User(username, password, con_password, head);
            boolean flag = service.findByName(user.getUsername());
            if (flag) {
                String fail = "用户名已存在，请重新输入";
                session.setAttribute("fail", fail);
                resp.sendRedirect("add.do");
            } else {
                boolean result = service.insert(user);
                if (!result) {
                    req.getRequestDispatcher("/manage/user/add.jsp").forward(req, resp);
                    return;
                }
                resp.sendRedirect("list.do");
            }

        } else if (path.equals("/list.do")) {
            List<User> userList = service.findAll();
            Integer currentPage = 1;
            if (req.getParameter("currentPage") != null) {
                currentPage = Integer.parseInt(req.getParameter("currentPage"));
            }
            Integer size = 2;
            Integer totalPage = userList.size() % 2 == 0 ? userList.size() / 2 : (userList.size() / 2 + 1);
            Integer prePage = currentPage - 1 <= 0 ? 1 : currentPage - 1;
            Integer nextPage = currentPage + 1 >= totalPage ? totalPage : currentPage + 1;
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", currentPage);
            req.setAttribute("prePage", prePage);
            req.setAttribute("nextPage", nextPage);
            Integer start = (currentPage - 1) * size;
            userList = service.findAll(start, size);
            req.setAttribute("userList", userList);
            req.getRequestDispatcher("/manage/user/list.jsp").forward(req, resp);
        } else if (path.equals("/update.do")) {
            Integer id = Integer.parseInt(req.getParameter("id"));
            User user = service.findById(id);
            req.setAttribute("user", user);
            req.getRequestDispatcher("/manage/user/update.jsp").forward(req, resp);
        } else if (path.equals("/updateAction.do")) {
            Integer id = Integer.parseInt(req.getParameter("id"));
            String username = req.getParameter("username");
            String nickname = req.getParameter("nickname");
            String tel = req.getParameter("tel");
            String email = req.getParameter("email");
            String new_password = req.getParameter("new_password");
            String password = req.getParameter("password");
            String head = "/message/home/img/4.jpeg";
            if (new_password == null || new_password.equals("")) {
                new_password = password;
            }
            User user = new User(id, username, nickname, new_password, tel, email, head);
            boolean result = service.update(user, id);
            if (!result) {
                resp.sendRedirect("/message/user/update.do?id=" + id);
                return;
            }
            resp.sendRedirect("list.do");
        } else if (path.equals("/delete.do")) {
            Integer id = Integer.parseInt(req.getParameter("id"));
            boolean result = service.delete(id);
            if (!result) {
                resp.sendRedirect("/message/user/delete.do?id=" + id);
                return;
            }
            resp.sendRedirect("list.do");
        }
    }
	
}
